<template>
	<view>
		<view class="head">
			<view class="line"></view>
			<view class="flow flex flai fljcsb">
				<view class="module">
					<text class="active">1</text>
					<text>上传认证</text>
				</view>


				<view class="module">
					<text :class="[status==1?'active':'']">2</text>
					<text>审核结果</text>
				</view>
			</view>
		</view>
		<view class="upload-box" v-if="isSubmit==0">
			<view class="up-box">
				<view class="prompt-one">上传营业执照</view>
				<view class="prompt-two">上传营业执照照片，图片大小不超过3M</view>
				<view class="up-area">
					<view class="card" @click="upPic(1)">
						<image src="../../static/img/license.png" mode="" v-if="dataForm.businessUrl==''"></image>
						<image :src="dataForm.businessUrl" mode="" v-else></image>
						<view class="up-title">上传营业执照</view>
					</view>
				</view>
			</view>
			<view class="up-box">
				<view class="prompt-one">上传身份证</view>
				<view class="prompt-two">上传二代身份证照片，图片大小不超过3M</view>
				<view class="up-area flex flai fljcsb">
					<view class="card" @click="upPic(2)">
						<image src="../../static/img/idCardBefore.png" mode="" v-if="dataForm.idcardFurl==''"></image>
						<image :src="dataForm.idcardFurl" mode="" v-else></image>
						<view class="up-title">上传人像页</view>
					</view>
					<view class="card"  @click="upPic(3)">
						<image src="../../static/img/idCardReverse.png" mode="" v-if="dataForm.idcardBurl==''"></image>
						<image :src="dataForm.idcardBurl" mode="" v-else></image>
						<view class="up-title">上传国徽页</view>
					</view>
				</view>
			</view>
		</view>
		<view class="status-box" v-if="isSubmit==1">
			<view class="statu-tab" v-if="auditState==0">
				<image src="../../static/img/auth_submit.png" mode=""></image>
				<view class="status-title">提交成功</view>
				<view class="status-prompt">已成功提交，请等待审核</view>
			</view>
			<view class="statu-tab" v-else-if="auditState==2">
				<image src="../../static/img/auth_fail.png" mode=""></image>
				<view class="status-title">认证失败</view>
				<view class="status-prompt">信息不准确，请重新上传后提交认证</view>
			</view>
			<view class="statu-tab" v-else>
				<image src="../../static/img/auth_right.png" mode=""></image>
				<view class="status-title">认证成功</view>
				<view class="status-prompt">恭喜您认证通过，成为商家</view>
			</view>
		</view>
		<view style="height:200rpx"></view>
		<view class="footer">
			<view class="up-btn" @click="upHand">提交认证</view>
		</view>
	</view>
</template>

<script>
	import API from '../../api/index.js'
	import requet from '../../api/request.js'
	
	export default {
		data() {
			return {
				status: 0,
				auditState: 0,
				isSubmit: 0,
				dataForm: {
					businessUrl: "", //营业执照图片
					idcardBurl: '', //身份证反面
					idcardFurl: "", //身份证正面
				}
			}
		},
		onLoad() {
			this.getAuthStatus()
			console.log(requet.requestUrl)
		},
		methods: {
			// 获取认证状态
			getAuthStatus() {
				//0待审核，1已审核，2已拒绝
				API.indexHome.queryAuth().then(res => {
					if (res.errno == '0') {
						this.auditState = res.data.auditStatus
					}
				})
			},
			// 上传图片
			upPic(type) {
				var _self  = this
				uni.chooseImage({
					count: 1,
					sizeType: ['copressed'],
					success(res) {
						//因为有一张图片， 输出下标[0]， 直接输出地址
						var imgFiles = res.tempFilePaths[0]
						console.log(imgFiles)
						// 上传图片
						// 做成一个上传对象
						var uper = uni.uploadFile({
							// 需要上传的地址
							url: requet.requestUrl +'/wx/storage/upload',
							// filePath  需要上传的文件
							filePath: imgFiles,
							name: 'file',
							success(res1) {
								// type 1 营业执照 2 身份证正面  3身份证反面
								// 显示上传信息
								console.log('上传成功')
								console.log(JSON.parse(res1.data))
								console.log(res1.data.url)
								var picUrl = JSON.parse(res1.data).data.url
								console.log(type)
								console.log(picUrl)
								if (type==1) {
									_self.dataForm.businessUrl = picUrl
								} else if (type==3) {
										_self.dataForm.idcardBurl = picUrl
								} else {
									_self.dataForm.idcardFurl = picUrl
								}
							}
						});
						// onProgressUpdate 上传对象更新的方法
						uper.onProgressUpdate(function(res) {
							// 进度条等于 上传到的进度
							_self.percent = res.progress
							console.log('上传进度' + res.progress)
							console.log('已经上传的数据长度' + res.totalBytesSent)
							console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend)
						})
					}
				})
			},
			submit() {
				if (this.dataForm.businessUrl=='') {
					uni.showToast({
						title:'请上传营业执照',
						icon:'none'
					})
					return
				} else if (this.dataForm.idcardBurl=='') {
					uni.showToast({
						title:'请上传身份证反面',
						icon:'none'
					})
					return
				} else if (this.dataForm.idcardFurl=='') {
					uni.showToast({
						title:'请上传身份证正面',
						icon:'none'
					})
					return
				} else {
				API.indexHome.submitAuth(this.dataForm).then(res => {
					if (res.errno == '0') {
						uni.showToast({
							title: '提交成功，请等待',
							duration: 2000
						});
						this.isSubmit = 1
						setTimeout(() => {
							wx.navigateBack()
						}, 2000)
					}
				})
			}
			}
		}
	}
</script>

<style>
	@import url("./index.css");
</style>
